package online.exam.controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import online.exam.pojo.AnswerPaper;
import online.exam.pojo.vo.AnswerPaperVO;
import online.exam.service.AnswerPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 王景煜
 */

@RestController
@RequestMapping("/answer")
public class AnswerPaperController {

    @Autowired
    private AnswerPaperService answerPaperServiceImpl;

    @PostMapping("/addAp")
    public int addAp(@RequestParam String apString) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, AnswerPaper.class);
        List<AnswerPaper> apList = mapper.readValue(apString, jt);
        return answerPaperServiceImpl.addAp(apList);
    }

    @GetMapping("/queryApByTestId")
    public List<AnswerPaperVO> queryApByTestId(@RequestParam String testId,
                                               @RequestParam(defaultValue = "") String stuId) {
        return answerPaperServiceImpl.queryApByTestId(testId, stuId);
    }

    @GetMapping("/queryTestPaperDetail")
    public Map<String, Object> queryTestPaperDetail(@RequestParam String testId,
                                                    @RequestParam(defaultValue = "") String clazz) {
        return answerPaperServiceImpl.queryTestPaperDetail(testId, clazz);
    }

}
